package br.com.ufpr.sgp.util.geral;

import org.apache.wicket.Component;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.behavior.AttributeAppender;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.model.Model;


public class JavascriptUtils {
    /**
     * Adiciona uma máscara com o maskPattern ao componente.
     * 
     * @param component
     *            componente
     * @param maskPattern
     *            pattern da máscara a ser aplicada no campo <br>
     * <br>
     *            <b>Mask Individual Character Usage:</b><br>
     *            <b>9</b> - designates only numeric values <br>
     *            <b>L</b> - designates only uppercase letter values <br>
     *            <b>l</b> - designates only lowercase letter values <br>
     *            <b>A</b> - designates only alphanumeric values <br>
     *            <b>X</b> - custom client script regular expression is specified</li> <br>
     *            All other characters are assumed to be "special" characters used to mask the input component <br>
     * <br>
     *            <b>Example 1:</b><br>
     *            (999)999-9999 only numeric values can be entered where the the character position value is 9. Parenthesis and dash are
     *            non-editable/mask characters. <br>
     * <br>
     *            <b>Example 2:</b><br>
     *            99L-ll-X[^A-C]X only numeric values for the first two characters, uppercase values for the third character, lowercase
     *            letters for the fifth/sixth characters, and the last character X[^A-C]X together counts as the eighth character regular
     *            expression that would allow all characters but "A", "B", and "C". Dashes outside the regular expression are
     *            non-editable/mask characters. <br>
     */
    public static void addMask(final TextField<?> component, final String maskPattern) {
        component.add(new MaskedInputBehavior(maskPattern));
    }

    /**
     * Adiciona o comando para exibir a modal com o gif e a mensagem de aguarde, após o clique em determinado componente.
     * 
     * @param comp
     * @param mensagem
     */
    public static void addAguardeOnClick(final Component comp, final String mensagem) {
        final StringBuffer msg = new StringBuffer();
        msg.append("<h4><br /><br /><img src=\"img/icon_fast.gif\" /><br /><center>" + mensagem + "</center><br /><br /></h4>");
        final String blockUI = "$.blockUI({ message: '" + msg.toString() + "', baseZ: 1000000});";
        comp.add(AttributeAppender.prepend("onclick", new Model<String>(blockUI)));
    }

    public static void finalizaAguarde(final AjaxRequestTarget target) {
        if (target != null) {
            target.appendJavaScript("try{$.unblockUI();}catch(e){};");
        }
    }

}
